package com.runvp.common.pay.adapay.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huifu.adapay.exception.BaseAdaPayException;
import com.huifu.adapay.model.DeviceInfo;
import com.huifu.adapay.model.payment.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jane.zhao
 */
public class PaymentDemo extends BaseDemo{
    /**
     * 运行支付类接口
     * @return paymentId
     * @throws Exception 异常
     */
    public static String executePaymentTest() throws Exception{
        //test chargeId = "002112019080716223300005091372336111616";
        PaymentDemo demo = new PaymentDemo();
        //支付接口
        Payment payment = demo.executePayment();
        System.out.println("支付:"+JSONObject.toJSONString(payment));
        //支付查询接口
        demo.queryPayment(payment.getId());
        //关单接口
        demo.closePayment(payment.getId());

        return payment.getId();
    }

    /**
     * 执行一个支付交易
     * @return 创建的支付对象
     * @throws Exception 异常
     */
    public Payment executePayment() throws Exception {
        System.out.println("=======execute payment begin=======");
        //创建支付对象的参数，全部参数请参考 https://docs.adapay.tech/api/04-trade.html#id3
        Map<String, Object> paymentParams = new HashMap<>(10);
        paymentParams.put("app_id", "app_d53f51a0-28cd-4948-ade2-eae5d343a0ea");
        paymentParams.put("order_no", "jsdk_payment_"+System.currentTimeMillis());
        paymentParams.put("pay_channel", PayChannelEnum.ALIPAY_QR.getCode());
//        paymentParams.put("pay_channel", PayChannelEnum.WX_LITE.getCode());
        paymentParams.put("pay_amt", "0.01");
        paymentParams.put("currency", CurrencyEnum.CNY.getCode());
        paymentParams.put("goods_title", "购买充电");
        paymentParams.put("goods_desc", "购买充电");

        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setDeviceType(DeviceTypeEnum.MOBILE.getCode());
        deviceInfo.setDeviceIp("127.0.0.1");
        paymentParams.put("device_info", deviceInfo);

        List<GoodsDetail> goodsDetailList = new ArrayList<>();
        GoodsDetail goodsDetail = new GoodsDetail();
        goodsDetail.setGoodsId("1");
        goodsDetail.setGoodsName("可乐");
        goodsDetail.setQuantity("1");
        goodsDetail.setPrice("100.00");
        goodsDetailList.add(goodsDetail);

        PromotionDetail promotionDetail = new PromotionDetail();
        promotionDetail.setGoodsDetail(goodsDetailList);
        promotionDetail.setCostPrice("100.00");

        PaymentExpend expend = new PaymentExpend();
        expend.setPromotionDetail(promotionDetail);
        expend.setBuyerId("2088012928900274");
        expend.setBuyerLogonId("");

        paymentParams.put("expend", expend);

        //调用sdk方法，创建支付，得到支付对象
        Payment payment = null;
        try {
            payment = Payment.create(paymentParams);
        } catch (BaseAdaPayException e) {
            e.printStackTrace();
        }
        System.out.println("payment result="+JSON.toJSONString(payment));
        return payment;
    }

    /**
     * 关闭一个支付交易
     * @param paymentId 要关闭的支付id
     * @return 关闭的支付对象
     * @throws Exception 异常
     */
    public Payment closePayment(String paymentId) throws Exception{
        System.out.println("=======close payment begin=======");
        //关闭支付对象的参数，全部参数请参考 https://docs.adapay.tech/api/04-trade.html#id11
        //调用sdk方法，关闭支付，得到支付对象
        Payment payment = null;
        try {
            payment = Payment.close(paymentId);
        } catch (BaseAdaPayException e) {
            e.printStackTrace();
        }
        System.out.println("close payment result="+JSON.toJSONString(payment));
        return payment;
    }

    /**
     * 查询一个支付交易
     * @param paymentId 要查询的支付id
     * @return 查询的支付对象
     * @throws Exception 异常
     */
    public Payment queryPayment(String paymentId) throws Exception{
        System.out.println("=======query payment begin=======");
        //查询支付对象的参数，全部参数请参考 https://docs.adapay.tech/api/04-trade.html#id7
        //调用sdk方法，查询支付交易，得到支付对象
        Payment payment = null;
        try {
            payment = Payment.query(paymentId);
        } catch (BaseAdaPayException e) {
            e.printStackTrace();
        }
        System.out.println("query payment result="+JSON.toJSONString(payment));
        return payment;
    }
}
